package com.dzt.androidframework.database.addedittask;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.EditText;

import com.dzt.androidframework.R;
import com.dzt.androidframework.fragment.FragmentBase;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by M02323 on 2017/6/12.
 */
@ContentView(R.layout.fragment_addtask)
public class AddEditFragment extends FragmentBase implements AddEditContract.View {
	public static final String ARGUMENT_EDIT_TASK_ID = "EDIT_TASK_ID";
	private AddEditContract.Presenter presenter;
	@ViewInject(R.id.add_task_title)
	private EditText etTitle;  //标题
	@ViewInject(R.id.add_task_description)
	private EditText etDescription;  //描述

	@Override
	protected void initWidgets() {
		FloatingActionButton fab =
				(FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task_done);
		fab.setImageResource(R.drawable.ic_done);
		fab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				presenter.saveTask(etTitle.getText().toString(),
						etDescription.getText().toString());
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		presenter.start();
	}

	@Override
	public void setPresenter(@NonNull AddEditContract.Presenter presenter) {
		this.presenter = checkNotNull(presenter);
	}

	@Override
	public void showEmptyTaskError() {
		showToast("任务不能为空");
	}

	@Override
	public void showTasksList() {
		getActivity().setResult(Activity.RESULT_OK);
		getActivity().finish();
	}

	@Override
	public void setTitle(String title) {
		etTitle.setText(title);
	}

	@Override
	public void setDescription(String description) {
		etDescription.setText(description);
	}

	@Override
	public boolean isActive() {
		return isAdded();
	}
}
